package cool.zhoujie.address.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import cool.zhoujie.address.R;
import cool.zhoujie.address.databinding.ListItemShowBinding;
import cool.zhoujie.address.db.Address;
import cool.zhoujie.address.viewmodel.ShowFragmentViewModel;

public class ShowItemAdapter extends RecyclerView.Adapter<ShowItemAdapter.ShowViewHolder> {

    private List<Address> mList;

    private ShowFragmentViewModel mViewModel;

    public ShowItemAdapter(List<Address> list,ShowFragmentViewModel viewModel) {
        mList = list;
        mViewModel = viewModel;
    }

    @NonNull
    @Override
    public ShowViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ListItemShowBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.list_item_show, parent, false);
        return new ShowViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ShowViewHolder holder, int position) {
        Address address = mList.get(position);
        holder.mBinding.setData(address);
        holder.mBinding.setViewmodel(mViewModel);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ShowViewHolder extends RecyclerView.ViewHolder {

        ListItemShowBinding mBinding;

        ShowViewHolder(@NonNull ListItemShowBinding binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }
    }
}
